// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// cSpell:ignore SOURCEVERSION, SOURCEBRANCH, BUILDID, BUILDNUMBER, COSMOSCLIENT, cosmosclient, libcosmosclient, cbindgen

fn main() {
    let build_id = format!(
        "$Id: {}, Version: {}, Commit: {}, Branch: {}, Build ID: {}, Build Number: {}, Timestamp: {}$",
        env!("CARGO_PKG_NAME"),
        env!("CARGO_PKG_VERSION"),
        option_env!("BUILD_SOURCEVERSION").unwrap_or("unknown"),
        option_env!("BUILD_SOURCEBRANCH").unwrap_or("unknown"),
        option_env!("BUILD_BUILDID").unwrap_or("unknown"),
        option_env!("BUILD_BUILDNUMBER").unwrap_or("unknown"),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs(),
    );
    println!("cargo:rustc-env=BUILD_IDENTIFIER={}", build_id);

    let mut header: String = r"// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// This file is auto-generated by cbindgen. Do not edit manually.
// cSpell: disable
"
    .to_string();
    header.push_str(&format!("// Build identifier: {}\n", build_id));

    let crate_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
    cbindgen::Builder::new()
        .with_crate(crate_dir)
        .with_language(cbindgen::Language::C)
        .with_after_include(format!(
            "\n// Specifies the version of cosmosclient this header file was generated from.\n// This should match the version of libcosmosclient you are referencing.\n#define COSMOSCLIENT_H_VERSION \"{}\"",
            env!("CARGO_PKG_VERSION")
        ))
        .with_cpp_compat(true)
        .with_header(header)
        .generate()
        .expect("unable to generate bindings")
        .write_to_file("include/cosmosclient.h");
}
